//
//  DbanView.m
//  engineeringDrawing
//
//  Created by BJB0004 on 17/4/11.
//  Copyright © 2017年 HangzhouBaixiongtechnologyco.,LTD. All rights reserved.
//

#import "DbanView.h"

@implementation DbanView{
    
    float hangy;//最后一行与有多少
    
    double yu2;//先铺短板的余数
    
    double yu1; //先铺长板的余数
    BOOL isD;  //最后一块带电1
    BOOL isD2; //偶数行最后一块是否带电
    
    BOOL zisD;  //奇数2快短板 最后一块带电1
    BOOL zisD2; //偶数2块短板 行最后一块是否带电
    int hang;
    int le1;
    int le2;
    
    double w;
    double h;
}

-(instancetype)initWithFrame:(CGRect)frame andWith:(double)with and:(double)height{
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];
    w = (double)(with/10.0);
    h = (double)(height/10.0) ;
    
    return self;
}

-(void)drawMyView{
    
    //地板
    [self drawDB];
    //红蓝双线
   
    
    
    
    
    
    
}
-(void)drawDB{
    CGContextRef  ctx= UIGraphicsGetCurrentContext();
    //
    [[UIColor grayColor]set];
    //    //画空心的四边形
    CGContextStrokeRect(ctx, CGRectMake(0, 0, w, h));
    //
    
    
    //    //渲染
    
    CGContextFillPath(ctx);
    hang =h/BWITH;
    NSLog(@"******%d",hang);
    hangy = (((int)(h*100))%YBWITH)/100;
    
    for (int i =1; i<=hang+1; i++) {
        CGContextMoveToPoint(ctx, 0, BWITH*i);
        //设置终点
        CGContextAddLineToPoint(ctx, w,BWITH*i);
        CGContextSetLineWidth(ctx, 1);
        [[UIColor blackColor] setStroke];
        
        CGContextStrokePath(ctx);
        if (i%2==0) {//偶数行
            //            NSLog(@"%d",i);
            CGContextMoveToPoint(ctx, HEIGHT, BWITH*(i-1));
            //设置终点
            CGContextAddLineToPoint(ctx, HEIGHT,BWITH*i);
            
            
            CGContextStrokePath(ctx);
            
            int num = (w - HEIGHT)/LHEIGHT;
            
            
            yu2 =(double)((((int)(w*100)) - YHEIGHT)%YLHEIGHT)*0.01;
            
            for (int j =1; j<=num+1; j++) {
                //设置起点
                CGContextMoveToPoint(ctx, HEIGHT+LHEIGHT*j, BWITH*(i-1));
                //设置终点
                CGContextAddLineToPoint(ctx, HEIGHT+LHEIGHT*j,BWITH*i);
                //                  [[UIColor orangeColor] setStroke];
              
                CGContextStrokePath(ctx);
                
            }
            
#warning 添加zisD!=0
            
            
            if (yu2>=HEIGHT&&yu2<HEIGHT*2) {//最后为一块带电短板
                CGContextMoveToPoint(ctx, LHEIGHT*num+HEIGHT*2, BWITH*(i-1) );
                //设置终点
                CGContextAddLineToPoint(ctx, LHEIGHT*num+HEIGHT*2,BWITH*i);
                [[UIColor blackColor] setStroke];
                
                CGContextStrokePath(ctx);
        
               
                
                isD2 = YES;
            }else if (yu2>HEIGHT*2){
#warning //偶数行 放两块短板
                
                zisD2 = YES;
                
                CGContextMoveToPoint(ctx, LHEIGHT*num+HEIGHT*2, BWITH*(i-1));
                //设置终点
                CGContextAddLineToPoint(ctx, LHEIGHT*num+HEIGHT*2,BWITH*i);
                [[UIColor blackColor] setStroke];
                
                CGContextStrokePath(ctx);
                
                CGContextMoveToPoint(ctx, LHEIGHT*num+HEIGHT*3, BWITH*(i-1) );
                //设置终点
                CGContextAddLineToPoint(ctx, LHEIGHT*num+HEIGHT*3,BWITH*i);
                [[UIColor blackColor] setStroke];
                
                CGContextStrokePath(ctx);
                
                                }
                
                
                
           
            
            
            //
            
            
        }else{//奇数行
            
            int num = w/LHEIGHT;
            yu1 =(double)(((int)(w*100)) %YLHEIGHT)*0.01;
            
            
            //                      int yu =(int)w %LHEIGHT;
            
            NSLog(@"%f",yu1);
            for (int j =1; j<=num; j++) {
                //设置起点
                CGContextMoveToPoint(ctx, LHEIGHT*j, BWITH*(i-1) );
                //设置终点
                CGContextAddLineToPoint(ctx, LHEIGHT*j,BWITH*i);
                //                [[UIColor greenColor] setStroke];
               
                CGContextStrokePath(ctx);
                
                
            }
            
            NSLog(@"%f====%f",yu1,HEIGHT);
            if (yu1>=HEIGHT&&yu1<HEIGHT*2) {//最后为一块带电短板
                CGContextMoveToPoint(ctx, LHEIGHT*(num)+HEIGHT, BWITH*(i-1));
                //设置终点
                CGContextAddLineToPoint(ctx, LHEIGHT*(num)+HEIGHT,BWITH*i);
                //                               [[UIColor greenColor] setStroke];
                
                CGContextStrokePath(ctx);
               
            }else if (yu1>=HEIGHT*2){
#warning 奇数最后为两块短板
                CGContextMoveToPoint(ctx, LHEIGHT*(num)+HEIGHT, BWITH*(i-1) );
                //设置终点
                CGContextAddLineToPoint(ctx, LHEIGHT*(num)+HEIGHT,BWITH*i);
                //                               [[UIColor greenColor] setStroke];
                
                CGContextStrokePath(ctx);
                CGContextMoveToPoint(ctx, LHEIGHT*(num)+HEIGHT*2, BWITH*(i-1) );
                //设置终点
                CGContextAddLineToPoint(ctx, LHEIGHT*(num)+HEIGHT*2,BWITH*i);
                //                               [[UIColor greenColor] setStroke];
                
                CGContextStrokePath(ctx);
                
                
                
            }
            
            
            
        }
        
    }
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
